package network;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

public class Receiver extends Thread{
	
	Protocol p;
	Socket socket;
	ObjectInputStream ois;
	public Receiver(Socket s, Protocol p) throws IOException{
		this.p = p;
		this.socket = s;
		this.ois = new ObjectInputStream(s.getInputStream());
	}
	
	public void run(){
		Object o;
		while(!socket.isClosed()){
			try {
				o = ois.readObject();
				//System.out.println("[Admin][Reception] " + o.getClass().getName());
				p.parse(o);
				
			} catch (java.io.EOFException e) {
				continue;
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				try {
					socket.close();
					ois.close();
				} catch (IOException e1) {}
				
				// On sort de la boucle
				break ;
			}
		}
	}
}
